package com.wchen.gdoc.generate.config;

import com.wchen.gdoc.generate.util.StringUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
 * 目标项目配置
 *
 * @author  wchen
 * @date    2019/11/11
 */
public class TargetProjectConfig {

    private String targetClassPath;
    private String targetProjectPath;
    private String targetScanPath;
    private String targetEntityPackage;
    private String filterEntity;
    //特殊类型处理 key 全限定名， value 展示的字段列表
    private Map<String, List<String>> exclusiveFieldMap = new HashMap<>();


    protected static TargetProjectConfig initTargetAnnotationConfig(Properties prop){
        TargetProjectConfig projectConfig = new TargetProjectConfig();
        projectConfig.setTargetClassPath(prop.getProperty(Constant.TARGET_BASE_CLASS_PATH));
        projectConfig.setTargetScanPath(prop.getProperty(Constant.TARGET_SCAN_PATH));
        String targetProjectPathConfig = prop.getProperty(Constant.TARGET_PROJECT_PATH);
        projectConfig.setTargetProjectPath(targetProjectPathConfig);
        projectConfig.setTargetEntityPackage(prop.getProperty(Constant.TARGET_ENTITY_PACKAGE));
        projectConfig.setFilterEntity(prop.getProperty(Constant.TARGET_FILTER_ENTITY));
        return projectConfig;
    }

    protected void validityCheck(){
        if(StringUtil.isBlank(targetClassPath) && StringUtil.isBlank(targetProjectPath)){
            throw new RuntimeException("targetClassPath or targetProjectPath need to configure one .");
        }

        if(StringUtil.isBlank(targetScanPath) || targetScanPath.split("\\.").length < 1){
            throw new RuntimeException("targetScanPath Not configured .");
        }
        if(StringUtil.isBlank(targetEntityPackage)){
            String[] paths = targetScanPath.split("\\.");
            this.targetEntityPackage = paths[0] + "." + paths[1];
        }
    }

    public String getTargetProjectPath() {
        return targetProjectPath;
    }

    public void setTargetProjectPath(String targetProjectPath) {
        String separator = System.getProperty("file.separator");
        if(!targetProjectPath.contains(":")){
            targetProjectPath = new StringBuilder(System.getProperty("user.dir"))
                    .append(separator)
                    .append(targetProjectPath)
                    .append(separator)
                    .append("src")
                    .append(separator)
                    .append("main")
                    .append(separator)
                    .append("java")
                    .append(separator)
                    .toString()
                    ;
        }
        this.targetProjectPath = targetProjectPath;
    }

    public String getTargetClassPath() {
        return targetClassPath;
    }

    public void setTargetClassPath(String targetClassPath) {
        this.targetClassPath = targetClassPath;
    }

    public String getTargetScanPath() {
        return targetScanPath;
    }

    public void setTargetScanPath(String targetScanPath) {
        this.targetScanPath = targetScanPath;
    }

    public String getTargetEntityPackage() {
        return targetEntityPackage;
    }

    public void setTargetEntityPackage(String targetEntityPackage) {
        this.targetEntityPackage = targetEntityPackage;
    }

    public String getFilterEntity() {
        return filterEntity;
    }

    public void setFilterEntity(String filterEntity) {
        this.filterEntity = filterEntity;
    }

    public Map<String, List<String>> getExclusiveFieldMap() {
        return exclusiveFieldMap;
    }

    public void setExclusiveFieldMap(Map<String, List<String>> exclusiveFieldMap) {
        this.exclusiveFieldMap = exclusiveFieldMap;
    }
}
